/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    bool isSymmetric(TreeNode* root)
    {
        
        if (i == 1)
        {
            i--;
          reverse(root->left);
        }

        if (!root)
            return true;
        return compare(root->left, root->right);
        
    }
    
    bool compare(TreeNode* node1, TreeNode* node2)
    {   
        if (node1 == nullptr && node2 == nullptr)
            return true;
        if (node1 == nullptr && node2 != nullptr)
            return false;
        if (node1 != nullptr && node2 == nullptr)
            return false;
        if (node1->val == node2->val)
        {
            return compare(node1->left, node2->left) && compare(node1->right, node2->right);
        }
        else
        {
            return false;
        }
        return true;
    }

    void reverse(TreeNode* root)
    {
        if (!root)
            return;
        swap(root->left, root->right);
        reverse(root->left);
        reverse(root->right);
    }

public:
int i = 1;

};